<template>
  <div class="register-container">
    <el-card class="register-box">
      <h2>用户注册</h2>
      <el-form
          ref="registerForm"
          :model="form"
          :rules="rules"
          @submit.prevent="handleRegister">

        <el-form-item prop="username">
          <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
          />
        </el-form-item>

        <el-form-item prop="userRealName">
          <el-input
              v-model="form.userRealName"
              placeholder="请输入真实姓名"
              prefix-icon="Avatar"
          />
        </el-form-item>

        <el-form-item prop="email">
          <el-input
              v-model="form.email"
              placeholder="请输入邮箱"
              prefix-icon="Message"
          />
        </el-form-item>

        <el-form-item prop="phone">
          <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
              prefix-icon="Iphone"
          />
        </el-form-item>

        <el-form-item prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio :label="1">女</el-radio>
            <el-radio :label="0">男</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-button
            type="primary"
            native-type="submit"
            class="register-btn">
          立即注册
        </el-button>

        <div class="login-link">
          已有账号？<el-link type="primary" @click="goLogin">立即登录</el-link>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import {userRegister} from "@/api/user.js";

const router = useRouter();

const form = ref({
  username: '',
  password: '',
  userRealName: '',
  email: '',
  phone: '',
  gender: 0,
  type: 2
});

const validatePhone = (rule, value, callback) => {
  const reg = /^1[3-9]\d{9}$/;
  if (!reg.test(value)) {
    callback(new Error('请输入有效的手机号码'));
  } else {
    callback();
  }
};

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 16, message: '长度在4到16个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '长度在6到18个字符', trigger: 'blur' }
  ],
  userRealName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
  ],
  phone: [
    { required: true, message: '请输入手机号码', trigger: 'blur' },
    { validator: validatePhone, trigger: 'blur' }
  ]
};

const handleRegister = async () => {
  try {
    console.log(form.value)
    const res =await userRegister(form.value);
    if (res.code === 200) {
      ElMessage.success('注册成功');
      await router.push('/login');
    } else {
      ElMessage.error(res.message);
    }
  } catch (error) {
    ElMessage.error('注册失败');
  }
};

const goLogin = () => {
  router.push('/login');
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #f0f2f5;
}

.register-box {
  width: 500px;
  padding: 20px;
}

.register-btn {
  width: 100%;
  margin-top: 10px;
}

.login-link {
  margin-top: 15px;
  text-align: center;
  color: #606266;
}
</style>
